﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace canyinguanlixitong
{
    public partial class zengjiaxiaofei : Form
    {
        public zengjiaxiaofei()
        {
            InitializeComponent();
        }
        public int czno;
        string constr = "Data Source=.;Initial Catalog=Supermarket management system;Integrated Security=True";
        bool boo = false;
        string sql;
        public int fuyuanId = 1;
        int money = 0;
        private void label1_Click(object sender, EventArgs e)
        {

        }
        DataSet ds = new DataSet("shangping1");
        
        private void zengjiaxiaofei_Load(object sender, EventArgs e)
        {
            ds = new DataSet("shangping");
            sql = @"SELECT  spID, spName, spJia
FROM      commodity";
            SqlConnection sqlcon = new SqlConnection(constr);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlcon);
            adapter.Fill(ds, "shangping");
            dataGridView1.DataSource = ds.Tables["shangping"];
            label6.Text = czno.ToString();
            label8.Text = Convert.ToString(dataGridView2.RowCount-1);
        }
        string str;
        private void dataGridView1_Click(object sender, EventArgs e)
        {
            
            str=Convert.ToString( dataGridView1.SelectedRows[0].Cells["xiangmuName"].Value);
            label11.Text = str;
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ds = new DataSet("shangping1");
            if (Convert.ToString( textBox1.Text)!=string.Empty)
            {
                if (ds.Tables["shangping1"]!=null)
                {
                    ds.Tables["shangping1"].Clear();
                }
                
                string str = textBox1.Text;
                sql = @"SELECT  spID, spName, spJia
FROM      commodity where spName like '" + str + "%'";
                SqlConnection sqlcon1 = new SqlConnection(constr);
                SqlDataAdapter adapter1 = new SqlDataAdapter(sql, sqlcon1);
                adapter1.Fill(ds, "shangping1");
                dataGridView1.DataSource = ds.Tables["shangping1"];
            }
                
            else{
                if (ds.Tables["shangping"]!=null)
                {
                    ds.Tables["shangping"].Clear();
                }
                
                sql = @"SELECT  spID, spName, spJia
FROM      commodity";
                SqlConnection sqlcon = new SqlConnection(constr);
                SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlcon);
                adapter.Fill(ds, "shangping");
                dataGridView1.DataSource = ds.Tables["shangping"];
                
            }
            
            
        }

      public  int i = 0;
        
        private void button1_Click(object sender, EventArgs e)
        {
            ds = new DataSet("dancan");
            if (ds.Tables["dancan"]!=null)
            {
                ds.Tables["dancan"].Clear();
            }
            string sql3 = @"select top 1 dcID from [Ordering management] 
order by dcID desc";
            SqlConnection sqlcon = new SqlConnection(constr);
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(sql3,sqlcon);
            int a = (int)sqlcom.ExecuteScalar();
            double danjia=Convert.ToDouble(dataGridView1.SelectedRows[0].Cells["danjia"].Value);
            
            string spid=Convert.ToString( dataGridView1.SelectedRows[0].Cells["xaingmuNO"].Value);
           //
            string sql2 = @"insert [Ordering management] (dcID,dcName,dcShul,dcUserTpye,dcQi,dcCzid,dcJine)
values(" + (a + 1) + ",'" + spid + "','" + Convert.ToInt32(textBox2.Text) + "','" + fuyuanId + "','null','" + czno + "'," + danjia + "*" + Convert.ToInt32(textBox2.Text) + ")";
            SqlCommand sqlcom1 = new SqlCommand(sql2, sqlcon);
            sqlcom1.ExecuteNonQuery();
            sqlcon.Close();
            string sql4 = @"SELECT top "+(++i)+ @" [Ordering management].dcID, commodity.spName, [Ordering management].dcShul, commodity.spJia, 
                   [Ordering management].dcUserTpye,[Ordering management].dcJine
FROM      commodity INNER JOIN
                   [Ordering management] ON commodity.spID = [Ordering management].dcName
order by dcID desc";
            SqlDataAdapter adapter = new SqlDataAdapter(sql4,sqlcon);
            adapter.Fill(ds,"diancan");
            dataGridView2.DataSource=ds.Tables["diancan"];
            for (int j = 0; j < dataGridView2.RowCount; j++)
            {
                if (Convert.ToString(dataGridView2.Rows[j].Cells["jine"].Value).Equals("0.0000"))
                {
                    dataGridView2.Rows[j].Cells["xiangmumingcheng"].Value += "(赠)";
                }
            }
            label8.Text = Convert.ToString(dataGridView2.RowCount - 1);
            MessageBox.Show("添加成功！");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            i = i - 1;
            string a = Convert.ToString(dataGridView2.SelectedRows[0].Cells["czhao"].Value);
            string sql1 = "delete from [Ordering management] where dcID=" +a+ "";
            ds.Tables["diancan"].Clear();
            SqlConnection sqlcon = new SqlConnection(constr);
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(sql1, sqlcon);
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            string sql4 = @"SELECT top " + i + @" [Ordering management].dcID, commodity.spName, [Ordering management].dcShul, commodity.spJia, 
                   [Ordering management].dcUserTpye,[Ordering management].dcJine
FROM      commodity INNER JOIN
                   [Ordering management] ON commodity.spID = [Ordering management].dcName
order by dcID desc";
            SqlDataAdapter adapter = new SqlDataAdapter(sql4, sqlcon);
            adapter.Fill(ds, "diancan");
            dataGridView2.DataSource = ds.Tables["diancan"];
            for (int j = 0; j < dataGridView2.RowCount; j++)
            {
                if (Convert.ToString(dataGridView2.Rows[j].Cells["jine"].Value).Equals("0.0000"))
                {
                    dataGridView2.Rows[j].Cells["xiangmumingcheng"].Value += "(赠)";
                }
            }
            label8.Text = Convert.ToString(dataGridView2.RowCount - 1);
            MessageBox.Show("退菜成功");
                
        }
        public string id;
        private void button5_Click(object sender, EventArgs e)
        {
          id= Convert.ToString(dataGridView2.SelectedRows[0].Cells["czhao"].Value);
            if (ds.Tables["diancan"]!=null)
            {
                ds.Tables["diancan"].Clear();
            }
            string sql = "update [Ordering management] set dcJine=0 where dcID="+id+"";
            SqlConnection sqlcon = new SqlConnection(constr);
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(sql,sqlcon);
            if (sqlcom.ExecuteNonQuery()>0)
            {
                MessageBox.Show("赠送成功");
//                string sql4 = @"SELECT top " + i + @" [Ordering management].dcID, commodity.spName, [Ordering management].dcShul, commodity.spJia, 
//                   [Ordering management].dcUserTpye,[Ordering management].dcJine
//FROM      commodity INNER JOIN
//                   [Ordering management] ON commodity.spID = [Ordering management].dcName
//order by dcID desc";
//                SqlDataAdapter adapter = new SqlDataAdapter(sql4, sqlcon);
//                adapter.Fill(ds, "diancan");
//                dataGridView2.DataSource = ds.Tables["diancan"];
//                for (int j = 0; j < dataGridView2.RowCount; j++)
//                {
//                    if (Convert.ToString(dataGridView2.Rows[j].Cells["jine"].Value).Equals("0.0000"))
//                    {
//                        dataGridView2.Rows[j].Cells["xiangmumingcheng"].Value += "(赠)";
//                    }
//                }
     
            }
            else
            {
                MessageBox.Show("赠送失败");
                string sql4 = @"SELECT top " + i + @" [Ordering management].dcID, commodity.spName, [Ordering management].dcShul, commodity.spJia, 
                   [Ordering management].dcUserTpye,[Ordering management].dcJine
FROM      commodity INNER JOIN
                   [Ordering management] ON commodity.spID = [Ordering management].dcName
where dcCzid="+czno+@"
order by dcID desc";
                SqlDataAdapter adapter = new SqlDataAdapter(sql4, sqlcon);
                adapter.Fill(ds, "diancan");
                dataGridView2.DataSource = ds.Tables["diancan"];
            }
            label8.Text = Convert.ToString(dataGridView2.RowCount - 1);

        }

        private void button6_Click(object sender, EventArgs e)
        {
            string dcid = Convert.ToString(dataGridView2.SelectedRows[0].Cells["czhao"].Value);
            zhuantai zhuan = new zhuantai();
            zhuan.dcid = dcid;
            zhuan.Show();
            i = i - 1;
        }

        private void zengjiaxiaofei_Activated(object sender, EventArgs e)
        {
            if (ds.Tables["diancan"]!=null)
            {
                ds.Tables["diancan"].Clear();
            }
            SqlConnection sqlcon = new SqlConnection(constr);
            string sql4 = @"SELECT top " + i + @" [Ordering management].dcID, commodity.spName, [Ordering management].dcShul, commodity.spJia, 
                   [Ordering management].dcUserTpye,[Ordering management].dcJine
FROM      commodity INNER JOIN
                   [Ordering management] ON commodity.spID = [Ordering management].dcName
where dcCzid=" + czno + @"
order by dcID desc";
                SqlDataAdapter adapter = new SqlDataAdapter(sql4, sqlcon);
                adapter.Fill(ds, "diancan");
                dataGridView2.DataSource = ds.Tables["diancan"];
           
            label8.Text = Convert.ToString(dataGridView2.RowCount - 1);
            money = 0;
            for (int j = 0; j < dataGridView2.RowCount; j++)
            {
                if (Convert.ToString(dataGridView2.Rows[j].Cells["jine"].Value).Equals("0.0000"))
                {
                    dataGridView2.Rows[j].Cells["xiangmumingcheng"].Value += "(赠)";
                }
                money += Convert.ToInt32(dataGridView2.Rows[j].Cells["jine"].Value);
            }
            label13.Text =Convert.ToString( money);
            label12.Text = Convert.ToString(money);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            bingkejiezhang jiesuan = new bingkejiezhang();
            jiesuan.czno = czno;
            jiesuan.Show();
            
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql = "update [Table]  set cz='1'   where czID='" + czno + "'";
            SqlConnection sqlcon = new SqlConnection(constr);
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            this.Close();
        }
        
    }
}
